package com.itheima.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.awt.geom.QuadCurve2D;
import java.util.List;

//关键点,还是记忆API
@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    /*@Autowired
    private IUserService userService;
    与下面的效果是一样的, 其实都是单例
    */
    private final IUserService userService;

    @ApiOperation("新增用户")
    @PostMapping
    public void saveUser(@RequestBody UserFormDTO userFormDTO) {
        //转换user ---> 属于: cn.hutool.core.bean包下面的
        User user = BeanUtil.copyProperties(userFormDTO, User.class);
        userService.save(user);
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/{id}")
    public void deleteUser(@PathVariable("id") Long id) {
        userService.removeById(id);
    }

    @ApiOperation("根据id查询")
    @GetMapping("/{id}")
    public UserVO queryById(@PathVariable("id") Long id) {
        /*User user = userService.getById(id);
        return BeanUtil.copyProperties(user, UserVO.class);*/
        return userService.queryUserAndAddressById(id);
    }

    @ApiOperation("根据id批量查询")
    @GetMapping
    public List<UserVO> queryByIds(@RequestParam("ids") List<Long> ids) {
        /*List<User> users = userService.listByIds(ids);
        return BeanUtil.copyToList(users, UserVO.class);*/
        return userService.queryUserAndAddressByIds(ids);
    }

//不需要请求路径中的动词来表示对资源的操作, 而是通过请求方式来表示对资源的处理, 这就是REST风格
    //接口上面一般,要写注释, 实现类一般不写, 因为可能有很多的实现类

    /**
     * 根据id扣减余额
     *
     * @param id
     * @param amount
     */
    @ApiOperation("根据id扣减余额")
    @PutMapping("/{id}/deduction/{amount}")
    public void deductBalanceById(@PathVariable("id") Long id, @PathVariable("amount") int amount) {
        //1、调用service层
        userService.deductBalanceById(id, amount);
    }


    @ApiOperation("根据条件查询用户列表")
    @PostMapping("/list")
    public List<UserVO> queryList(@RequestBody UserQuery userQuery) {
        String username = userQuery.getName();
        Integer status = userQuery.getStatus();
        Integer maxBalance = userQuery.getMaxBalance();
        Integer minBalance = userQuery.getMinBalance();
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        /*
        // 最原始的
        if (StrUtil.isNotBlank(username)){
            userQueryWrapper.lambda().like(User::getUsername, username);
        }*/


       /*
       第一次演化
        userQueryWrapper.lambda()
                //成立条件: 字段: 查询的关键字; 如果第一个值为true才会设置当前这个条件到where中
                .like(StrUtil.isNotBlank(username), User::getUsername, username)
                .eq(status != null, User::getStatus, status)
                .le(maxBalance != null, User::getBalance, maxBalance) // 小于等于
                .ge(minBalance != null, User::getBalance, minBalance);// 大于等于

        List<User> userList = userService.list(userQueryWrapper);*/

        // 链式查询
        List<User> userList = userService.lambdaQuery()
                .like(StrUtil.isNotBlank(username), User::getUsername, username)
                .eq(status != null, User::getStatus, status)
                .le(maxBalance != null, User::getBalance, maxBalance) // 小于等于
                .ge(minBalance != null, User::getBalance, minBalance)// 大于等于
                .list(); // 最终进行查询 --> 这个用的更多

        return BeanUtil.copyToList(userList, UserVO.class);
    }

}
